//
//  LYMineCollectVidoViewController.m
//  artist
//
//  Created by Jay_Apple on 16/5/8.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYMineCollectVidoViewController.h"
#import "LYMineVideoCell.h"
#import "LYCollectListModel.h"
#import "LYVideoWebViewController.h"
@interface LYMineCollectVidoViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSArray *models;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation LYMineCollectVidoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self loadVideoData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 网络请求
/**
 *  课程数据
 */
- (void)loadVideoData{
    
    [MBProgressHUD showMessage:nil];
    [LYRequestTool getMyCollectionWithToken:LYToken type:ObjectTypeVideo success:^(NSArray *models) {
        [MBProgressHUD hideHUD];
        self.models = models;
        [self.tableView reloadData];

    } failure:^(NSString *code, NSString *message) {
        [MBProgressHUD hideHUD];
    }];
    
}

/**
 *  取消收藏
 */
- (void)cancelCollectWithTopicID:(NSString *)topicID andIndexPath:(NSIndexPath *)indexPath{
    
    
    [LYRequestTool cancelCollectionWithToken:LYToken type:ObjectTypeVideo data_id:topicID success:^{
        
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
    } failure:^(NSString *code, NSString *message) {
        [MBProgressHUD showOnlyText:message];
    }];
    
}



#pragma mark - tableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 80;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LYMyVideoListModel *model = self.models[indexPath.row];
    LYVideoWebViewController *videoVC = [LYVideoWebViewController videoWebVCWithID:model.Id];
    [self.navigationController pushViewController:videoVC animated:YES];
    
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LYMyVideoListModel *model = self.models[indexPath.row];
    
    //判断编辑模式是否为delete
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self cancelCollectWithTopicID:model.Id andIndexPath:indexPath];
        
    }
}


#pragma mark - tableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.models.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"LYMineVideoCell";
    LYMineVideoCell *cell = [LYMineVideoCell cellWithTableView:tableView andIndetifier:cellID];
    
    //model
    LYMyVideoListModel *model = self.models[indexPath.row];
    cell.model = model;
    
    return cell;
    
}


@end
